package com.hx.miniblog.controller.business;

import cn.hutool.core.util.RandomUtil;
import com.hx.miniblog.annotation.CheckLogin;
import com.hx.miniblog.annotation.PassToken;
import com.hx.miniblog.controller.BaseController;
import com.hx.miniblog.pojo.SysRole;
import com.hx.miniblog.pojo.SysUser;
import com.hx.miniblog.pojo.Weather;
import com.hx.miniblog.pojo.mj.RoleType;
import com.hx.miniblog.pojo.ro.LoginRo;
import com.hx.miniblog.pojo.vo.LoginAccountVo;
import com.hx.miniblog.service.SysRoleService;
import com.hx.miniblog.service.SysUserService;
import com.hx.miniblog.util.Result;
import com.hx.miniblog.util.utils.APIUtils;
import com.hx.miniblog.util.utils.WebSocketUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.time.DayOfWeek;
import java.time.LocalDateTime;
import java.time.Month;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yangGuoQing 2021/12/28 15:05
 */
@RestController
@Api(value = "HealthController",tags = "服务健康检查接口")
@RequestMapping("/api/health")
@Slf4j
public class HealthController extends BaseController {
    @Resource
    SysUserService userService;

    @Resource
    SysRoleService roleService;

    @Resource
    protected WebSocketUtil webSocketUtil;

    @PostMapping("/ping")
    @ApiOperation(value = "健康检查ping")
    @PassToken
    public Result<String> testSwagger(){
        return Result.OK("ping");
    }
    @PostMapping("/pong")
    @ApiOperation(value = "健康检查pong")
    @CheckLogin
    public Result<LoginAccountVo> testCurrentUser(){
        System.out.println(currentUser());
        return Result.OK(currentUser());
    }

    @PostMapping("/pong2")
    @ApiOperation(value = "健康检查pong2")
    @CheckLogin(roles = RoleType.ADMIN)
    public Result<LoginAccountVo> testAdmin(){
        System.out.println(currentUser());
        return Result.OK(currentUser());
    }

    @PostMapping("/sendMessage")
    @ApiOperation(value = "webSocket发消息")
    @PassToken
    public Result<Void> sendMessage(){
        webSocketUtil.AppointSending("123","Nihao");
        log.info("用户123是否在线？：{}",webSocketUtil.checkUserOnline("123"));
        log.info("用户369是否在线？：{}",webSocketUtil.checkUserOnline("369"));
        webSocketUtil.sendAll("你们好");
        webSocketUtil.sendAll();
        return Result.OK();
    }
    @PostMapping("/addAccount")
    @ApiOperation(value = "添加一个用户")
    @PassToken
    public Result<Void> addAccount(LoginRo ro){
        String id = RandomUtil.randomNumbers(12);
        userService.save(SysUser.builder().userName(ro.getUserName()).userAvatar("meiyou").userPassword(ro.getPassword()).id(id).build());
        roleService.save(SysRole.builder().roleType(RoleType.ADMIN).userId(id).build());
        return Result.OK();
    }
    @PostMapping("/helloDocker")
    @ApiOperation(value = "helloDocker")
    @PassToken
    public Result<String> helloDocker(){
        return Result.OK("hi");
    }

    @GetMapping("/baby")
    @ApiOperation(value = "baby")
    @PassToken
    public Result<Map<String,String>> helloBaby() throws ParseException {
        return Result.OK(APIUtils.collectMessage());
    }

    @GetMapping("/push")
    @ApiOperation(value = "push")
    @PassToken
    public Result<Void> push() throws ParseException {
        APIUtils.push();
        return Result.OK();
    }

}
